package com.ytkj.digitalConstruction.service.constructionprocess;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ytkj.digitalConstruction.entity.user.User;
import com.ytkj.digitalConstruction.enums.ResultEnum;
import com.ytkj.digitalConstruction.enums.construction.ConstructionResultEnum;
import com.ytkj.digitalConstruction.exception.CustomException;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.resbean.FlowProcessTemp;
import com.ytkj.digitalConstruction.service.user.UserService;

/**
 * @Description: 施工流程用到的工具
 * @ClassName: ConstructionUtilService 
 * @author 宁煜
 * @date 2017年12月26日 下午5:51:13 
 *
 */
@Service
public class ConstructionUtilService {

	private static UserService userService;
	
	@Autowired
	public void setUserService(UserService userService) {
		ConstructionUtilService.userService = userService;
	}

	/**
	 * @Description: 获取当前进度
	 * @Title: getNowProcess 
	 * @param @param temps
	 * @param @return 
	 * @return Integer
	 * @throws
	 */
	public static Integer getNowProcess(List<FlowProcessTemp> temps) {
		for(int i=temps.size()-1;i>=0;i--) {
			if(temps.get(i).getState() == 1) {
				return temps.get(i).getFlowId();
			}
		}
		return -1;
	}
	/**
	 * @Description: 获取当前进度
	 * @Title: getNowProcess 
	 * @param @param temps
	 * @param @return    设定文件 
	 * @return Integer    返回类型 
	 * @throws
	 */
	public static FlowProcessTemp getNowProcessBase(List<FlowProcessTemp> temps) {
		for(int i=temps.size()-1;i>=0;i--) {
			if(temps.get(i).getState() == 1) {
				return temps.get(i);
			}
		}
		return null;
	}
	
	/**
	 * @Description: 判断能否转成数字
	 * @Title: isChangeInt 
	 * @param @param str
	 * @param @return 
	 * @return boolean
	 * @throws
	 */
	public static boolean isNumber(String str) {
		   Pattern pattern = Pattern.compile("[0-9]*"); 
		   Matcher isNum = pattern.matcher(str);
		   return isNum.matches(); 
	}
	
	/**
	 * @Description: 增加后根据自增id判断是否增加成功
	 * @Title: isAddSuccess 
	 * @param @param id 
	 * @return void
	 * @throws
	 */
	public static void isAddSuccess(Object id){
		if(id == null){
			throw new CustomException(ConstructionResultEnum.FLOW_ADD_ERROR);
		}
	}
	
	/**
	 * @Description: 修改后根据操作行数判断是否修改成功
	 * @Title: isUpdateSuccess 
	 * @param @param result 
	 * @return void
	 * @throws
	 */
	public static void isUpdateSuccess(Integer result){
		if(result == null || result == 0){
			throw new CustomException(ConstructionResultEnum.FLOW_UPDATE_ERROR);
		}
	}
	
	/**
	 * @Description: 根据webToekn获取User信息
	 * @Title: getUserByRequest 
	 * @param @param request
	 * @param @return 
	 * @return User
	 * @throws
	 */
	public static User getUserByRequest(HttpServletRequest request) {
		 String webToken = request.getHeader("x-access-token");
		 User user = userService.getUserByWebToken(webToken);
		 if(user == null) {
			 throw new CustomException(ResultEnum.LOGIN_OUT_TIME);
		 }
		 return user; 
		/*User user = new User();
		user.setUserId(1);
		return user;*/
	}
	
	/**
	 * @Description: 将带,的字符串转化为Integer集合
	 * @Title: stringChangeIntegerList 
	 * @param @param userIds
	 * @param @return 
	 * @return List<Integer>
	 * @throws
	 */
	public static List<Integer> stringChangeIntegerList(String users){
		List<Integer> list = new ArrayList<>();
		String[] userIds = users.split(",");
		for(String userId : userIds) {
			list.add(Integer.parseInt(userId));
		}
		return list;
	}
	
	/**
	 * @Description: 多个str合成一个
	 * @Title: synthesisString 
	 * @param @param strings
	 * @param @return    设定文件 
	 * @return String    返回类型 
	 * @throws
	 */
	public static String synthesisString(String... strings) {
		StringBuffer buffer = new StringBuffer("");
		for(String str : strings) {
			buffer.append(str);
		}
		return buffer.toString();
	}
	
	/**
	 * @Description: 判断某日期是否是当日的
	 * @Title: checkNowDate 
	 * @param @param checkDate
	 * @param @return    设定文件 
	 * @return boolean    返回类型 
	 * @throws
	 */
	public static boolean checkNowDate(Date checkDate) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(new Date()).equals(dateFormat.format(checkDate));
	}
	
}
